#!/bin/bash
###
 # @Author: MingRui_Du 2874810192@qq.com
 # @Date: 2024-12-24 07:35:00
 # @LastEditors: MingRui_Du 2874810192@qq.com
 # @LastEditTime: 2024-12-24 08:14:23
 # @FilePath: \dev\centos\install_toolchain.sh
 # @Description: 
### 

# 创建 downloads 文件夹
create_downloads_directory() {
    if [ ! -d "downloads" ]; then
        echo "创建 downloads 文件夹..."
        mkdir -p downloads
        if [ $? -eq 0 ]; then
            echo "downloads 文件夹创建成功。"
        else
            echo "创建 downloads 文件夹失败。请手动创建。"
            exit 1
        fi
    else
        echo "downloads 文件夹已存在。"
    fi
}

# 检查并安装必要的工具链
install_toolchain() {
    echo "正在更新包列表..."
    sudo yum update -y

    # 首先安装 curl
    if ! command -v curl &> /dev/null; then
        echo "curl 未安装。正在安装..."
        sudo yum install -y curl
        if command -v curl &> /dev/null; then
            echo "curl 安装成功。"
        else
            echo "安装 curl 失败。请手动安装。"
            exit 1
        fi
    else
        echo "curl 已成功安装。"
    fi

    # 安装基本工具链
    packages=(
        gcc
        gcc-c++
        cmake
        make
        poco-devel
        fltk-devel
        cairo-devel
        mesa-libGL-devel
        doxygen
        zlib-devel
        libjpeg-turbo-devel
        libpng-devel
        pkgconfig
        git
        autoconf
        automake
        libtool
    )

    echo "正在安装必要的软件包..."
    sudo yum install -y "${packages[@]}"

    echo "工具链安装完成。"
}

# 下载 libressl 源代码
download_libressl_source() {
    local libressl_version="4.0.0"  # 你可以根据需要更改版本号
    local libressl_url="https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-${libressl_version}.tar.gz"
    local libressl_file="downloads/libressl-${libressl_version}.tar.gz"

    if [ ! -f "$libressl_file" ]; then
        echo "下载 libressl 源代码..."
        curl -L -o "$libressl_file" "$libressl_url"
        if [ $? -eq 0 ]; then
            echo "libressl 源代码下载成功。"
        else
            echo "libressl 源代码下载失败。请手动下载。"
            exit 1
        fi
    else
        echo "libressl 源代码已存在。"
    fi
}

# 编译 libressl
compile_libressl() {
    local libressl_version="4.0.0"
    local libressl_dir="downloads/libressl-${libressl_version}"

    if [ ! -d "$libressl_dir" ]; then
        echo "解压 libressl 源代码..."
        tar -xzf "downloads/libressl-${libressl_version}.tar.gz" -C downloads
        if [ $? -ne 0 ]; then
            echo "解压 libressl 源代码失败。"
            exit 1
        fi
    fi

    cd "$libressl_dir" || exit 1

    echo "配置 libressl..."
    ./config --prefix=/usr/local/libressl
    if [ $? -ne 0 ]; then
        echo "配置 libressl 失败。"
        exit 1
    fi

    echo "编译 libressl..."
    make -j$(nproc)
    if [ $? -ne 0 ]; then
        echo "编译 libressl 失败。"
        exit 1
    fi

    echo "安装 libressl..."
    sudo make install
    if [ $? -ne 0 ]; then
        echo "安装 libressl 失败。"
        exit 1
    fi

    echo "libressl 编译和安装完成。"
    cd - || exit 1
}

# 下载 curl 源代码
download_curl_source() {
    local curl_version="8.11.1"  # 你可以根据需要更改版本号
    local curl_url="https://curl.se/download/curl-${curl_version}.tar.gz"
    local curl_file="downloads/curl-${curl_version}.tar.gz"

    if [ ! -f "$curl_file" ]; then
        echo "下载 curl 源代码..."
        curl -L -o "$curl_file" "$curl_url"
        if [ $? -eq 0 ]; then
            echo "curl 源代码下载成功。"
        else
            echo "curl 源代码下载失败。请手动下载。"
            exit 1
        fi
    else
        echo "curl 源代码已存在。"
    fi
}

# 编译 curl 使用 libressl
compile_curl() {
    local curl_version="8.11.1"
    local curl_dir="downloads/curl-${curl_version}"

    if [ ! -d "$curl_dir" ]; then
        echo "解压 curl 源代码..."
        tar -xzf "downloads/curl-${curl_version}.tar.gz" -C downloads
        if [ $? -ne 0 ]; then
            echo "解压 curl 源代码失败。"
            exit 1
        fi
    fi

    cd "$curl_dir" || exit 1

    echo "配置 curl 使用 libressl..."
    ./configure --with-ssl=/usr/local/libressl
    if [ $? -ne 0 ]; then
        echo "配置 curl 失败。"
        exit 1
    fi

    echo "编译 curl..."
    make -j$(nproc)
    if [ $? -ne 0 ]; then
        echo "编译 curl 失败。"
        exit 1
    fi

    echo "安装 curl..."
    sudo make install
    if [ $? -ne 0 ]; then
        echo "安装 curl 失败。"
        exit 1
    fi

    echo "curl 编译和安装完成。"
    cd - || exit 1
}

# 下载 Crow 源代码
download_crow_source() {
    local crow_repo="https://gitee.com/MingRui_Du/crow.git"
    local crow_dir="downloads/crow"

    if [ ! -d "$crow_dir" ]; then
        echo "下载 Crow 源代码..."
        git clone "$crow_repo" "$crow_dir"
        if [ $? -eq 0 ]; then
            echo "Crow 源代码下载成功。"
        else
            echo "Crow 源代码下载失败。请手动下载。"
            exit 1
        fi
    else
        echo "Crow 源代码已存在。"
    fi
}

# 编译 Crow 使用 libressl
compile_crow() {
    local crow_dir="downloads/crow"

    cd "$crow_dir" || exit 1

    echo "配置 Crow..."
    mkdir -p build
    cd build || exit 1

    # 设置 CMake 变量以使用 libressl
    cmake .. -DOPENSSL_ROOT_DIR=/usr/local/libressl
    if [ $? -ne 0 ]; then
        echo "配置 Crow 失败。"
        exit 1
    fi

    echo "编译 Crow..."
    make -j$(nproc)
    if [ $? -ne 0 ]; then
        echo "编译 Crow 失败。"
        exit 1
    fi

    echo "Crow 编译完成。"
    cd - || exit 1
}

# 创建 downloads 文件夹
create_downloads_directory

# 执行安装
install_toolchain

# 下载 libressl 源代码
download_libressl_source

# 编译 libressl
compile_libressl

# 下载 curl 源代码
download_curl_source

# 编译 curl 使用 libressl
compile_curl

# 下载 Crow 源代码
download_crow_source

# 编译 Crow 使用 libressl
compile_crow

echo "当前日期和时间是："
date
echo "当前目录下的文件和文件夹有："
ls -l